//
//  Ext_UIBarButtonItem.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 17/1/17.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit

extension UIColor {

    convenience init(r:CGFloat, g:CGFloat, b:CGFloat, alpha:CGFloat) {
        
         self.init(red: r / 255, green: g / 255.0 , blue: b / 255, alpha: alpha)
    }
}

extension UIBarButtonItem {

   
    //便利构造函数
    convenience init(imgName:String? = nil, title:String? = nil, target:Any?, action:Selector ) {
        
        self.init()
        
        let btn = UIButton()
        //图片不为空
        if let img = imgName {
            btn.setImage(UIImage(named:img), for: UIControlState.normal)
            btn.setImage(UIImage(named:"\(img)_highlighted"), for: UIControlState.highlighted)
        }
        
        if let tit = title {
            
            btn.setTitle(tit, for: UIControlState.normal)
            btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
            btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        }
        
        btn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: midTextFont)
        btn.sizeToFit()
        customView = btn
    }
    
    
}
